package com.whfc.wxmp.api.base.controller;

import com.whfc.base.service.SmsService;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.wxmp.annotation.CurrentWxUser;
import com.whfc.fuum.entity.WxUser;
import com.whfc.wxmp.util.RequestConetxtUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;

/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2019/7/31 16:10
 */
@RestController
@RequestMapping("/mp/api/sms")
@Validated
public class WxSmsController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @DubboReference(interfaceClass = SmsService.class, version = "1.0.0")
    private SmsService smsService;

    @PostMapping("sendCode")
    public Result sendCode(@CurrentWxUser WxUser loginUser,
                           @NotEmpty @RequestParam("phone") String phone) {
        logger.info("WXMPUA|{}|{}|{}|{}", RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "发送手机验证码", RequestConetxtUtil.getParameters());
        smsService.sendSmsCode(phone);
        return ResultUtil.success();
    }

    @PostMapping("checkCode")
    public Result checkCode(@CurrentWxUser WxUser loginUser,
                            @NotEmpty @RequestParam("phone") String phone, @NotEmpty @RequestParam("code") String code) {
        logger.info("WXMPUA|{}|{}|{}|{}", RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "校验手机验证码", RequestConetxtUtil.getParameters());
        smsService.checkSmsCode(phone, code);
        return ResultUtil.success();
    }
}
